﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeTest.aspx.cs" Inherits="WebNormal.TreeTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>

    <script src="JS/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="JS/oisSimpleTree/OISSimpleTree.js" type="text/javascript"></script>
    <link href="JS/oisSimpleTree/OISSimpleTree.css" rel="stylesheet" type="text/css" />
    
    
    <script type="text/javascript">

        $(function() {
            createTree("root1", "主机");
            AddOneLevelNode("root1");  
        })


        function createTree(id, desc) {
            debugger;
            var hid = id.split("_")[1]
            //var url = "../common/Nheadform.aspx?gid=MAC0103f&hid='" + hid + "'";
            var url = "MAC0106f.aspx?hid='" + hid + "'";
            var tree = new OISTree(id, desc, "JS/simpleTree/img/base.gif", "divTree", "root", url);


            tree.TreeNodeClick = TreeNodeClick;
        }


        function TreeNodeClick(Node) {

        }
        //添加节根点下面的一级节点。
        function AddOneLevelNode(treeRootID) {
            var hid = treeRootID.split("_")[1]
            addNode(treeRootID + "_siteList", treeRootID, "網站", "JS/simpleTree/img/globe.gif", "siteList", "../common/formhandle.aspx?gid=MAC0207g&macid=" + hid);
            addNode(treeRootID + "_ServiceList", treeRootID, "服務", "JS/simpleTree/img/globe.gif", "ServiceList", "../common/formhandle.aspx?gid=MAC0204g&macid=" + hid);
            addNode(treeRootID + "_DBList", treeRootID, "資料庫", "JS/simpleTree/img/globe.gif", "DBList", "../common/formhandle.aspx?gid=MAC0208g&macid=" + hid);
            addNode(treeRootID + "_DriverList", treeRootID, "硬碟", "JS/simpleTree/img/globe.gif", "DriverList", "../common/formhandle.aspx?gid=MAC0205g&macid=" + hid);


        }


        //回调函数。
        function callbackFunction() { }

        //强制刷新
        function forceLoad(Node) {
            //删除下面的所有子节点。
            removeSubNode(Node.getID());
            var id = Node.getID();
            var type = Node.getType();

            if (type == "root") {
                AddOneLevelNode(id);
            }

            //注册回调函数，在查询完子级以后再自动查询孙级。

            if (type == "root" || type == "siteList" || type == "ServiceList" || type == "DBList" || type == "DriverList" || type == "virDir") {

                callbackFunction = function() {
                    TreeNodeLoad(Node, true);
                }

                queryData(type, id);
            }
        }
        function TreeNodeLoad(Node, isForce) {
            return;
            var title = Node.getTitle();
            var id = Node.getID();
            var type = Node.getType();
            var isLoad = Node.getIsLoad();
            if (isLoad == "false" || isForce) {
                queryGrandsonNode(type, id);
                setIsLoad(id);
            }
        }


        function queryGrandsonNode(type, nodeId) {
            var nodeColl = $("div[@parentId=" + nodeId + "]");
            if (type == "root" || type == "siteList") {
                nodeColl.each(function(i) {
                    queryData(this.type, this.id);
                });
            }
        }

        function queryData(type, id) {
            var hidVirDirName = document.getElementById("hidVirDirName");
            //alert(hidVirDirName.value);
            var url = hidVirDirName.value + "/getMachineInfo";
            //var url = "/jgoidc/MAC/MAC0106g.aspx/getMachineInfo";

            var begmacid;
            var endmacid;
            var roomid;

            begmacid = document.getElementById("begmacid");
            endmacid = document.getElementById("endmacid");
            roomid = document.getElementById("roomid");

            var data = "{'type':\"" + type + "\",'id':\"" + id + "\"  ,'begmacid':\"" + begmacid.value + "\"  ,'endmacid':\"" + endmacid.value + "\" ,'roomid':\"" + roomid.value + "\"}";
            $.ajax({
                type: "POST",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: successFunction,
                error: errorFunction
            });
        }


        function successFunction(data) {
            var obj = eval(data.d);
            if (obj == undefined) {
                return;
            }
            var parentId = obj[0].parentId;
            var type = obj[0].type;

            //setIsLoad(parentId); //标识为已加载过。

            if (obj.length <= 1) {
                //则将当前节点标识为展开状态。
                setNodeIsOpen(parentId);
            }


            if (type == "machine") {
                for (var i = 1; i < obj.length; i++) {
                    var hid = obj[i].hid;

                    var treeRootID = "OISTree_" + hid;
                    var desc = obj[i].desc;
                    createTree(treeRootID, desc);
                    //在根下面添加服务，硬盘，DB等节点。
                    AddOneLevelNode(treeRootID);

                    ShowNode(treeRootID); //折叠节点
                }
            }

            else if (type == "siteList") {
                for (var i = 1; i < obj.length; i++) {
                    var hids = parentId.split("_");
                    var macid = hids[1];
                    var siteid = obj[i].id;
                    addNode(parentId + "_" + obj[i].id, parentId, obj[i].desc, "img/site.gif", "virDir", "../common/NEditForm.aspx?gid=MAC0209f&macid='" + macid + "'&siteid='" + siteid + "'");
                }
            }

            else if (type == "DBList") {
                if (obj.length > 1) {
                    var subQty = obj[1].qty;
                    updateParentQty(parentId, subQty);
                }
            }

            else if (type == "ServiceList") {
                for (var i = 1; i < obj.length; i++) {
                    var id = parentId + "_" + obj[i].id;
                    addNode(id, parentId, obj[i].desc, "img/Service.gif", "Service");
                    setLeafNode(id);
                }
            }
            else if (type == "virDir") {
                for (var i = 1; i < obj.length; i++) {
                    var id = parentId + "_" + obj[i].id;
                    addNode(id, parentId, obj[i].desc, "img/virDir.gif", "Dir");
                    setLeafNode(id);
                }

            }
            else if (type == "DriverList") {
                for (var i = 1; i < obj.length; i++) {
                    var id = parentId + "_" + obj[i].id;
                    addNode(id, parentId, obj[i].desc, "img/Driver.gif", "Driver");
                    setLeafNode(id);
                }
            }

            //根节点不需要更新。
            if (type != "machine" && type != "root" && type != "DBList") {
                //更新父级的数量统计，
                updateParentQty(parentId, obj.length - 1);
                //刷新节点状态。
                CloseNode(parentId);
            }

            //执行回调函数，如何有的话。
            if (callbackFunction != null) {
                callbackFunction();
            }
            callbackFunction = null;
            //window.callbackFunction = undefined;

        }

        //更新父级上面的数量显示，
        function updateParentQty(parentId, num) {
            var obj = $("#" + parentId).find("span[class=SubCount]")[0];
            if (obj != undefined && obj != null) {
                obj.innerHTML = " [" + num + "]";
            }
        }

        function queryGrandsonNode(type, nodeId) {
            var nodeColl = $("div[@parentId=" + nodeId + "]");
            if (type == "root" || type == "siteList") {
                nodeColl.each(function(i) {
                    queryData(this.type, this.id);
                });
            }
        }


        function errorFunction(data) {
            alert(data.ResponseText);
        }
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
    <table>
            <tr>
                <td width="20"></td>
                <td>主機編號</td>
                <td>
                    <input id="begmacid" runat="server" Width="100px"/></td>
                <td>~</td>
                <td><input id="endmacid" runat="server" Width="100px"/></td>
            </tr>
            <tr>
                <td width="20"></td>
                <td>機房</td>
                <td>
                    <input id="roomid" runat="server" />
                </td>
                <td></td>
                <td></td>
            </tr>
        </table>
        
       
        <br />
        
        <div id="divTree" class="TreeRoot" style="margin-left:20px; font-size:12px;"></div>
        <input type="hidden" id="hidVirDirName" runat="server" /><!-- 强制刷新时用到-->
    
    
    </div>
    </form>
</body>
</html>
